5B - Center Alignment - CodeForces Solution


implementation strings *1200

Please click on ads to support us..

Python Code:

import sys
a=[]
mx=0
while True:
    try:
        i=input()
        
        start=0
        end=len(i)-1
        while i and i[start]==' ':
            start+=1
        while i and i[end]==' ':
            end-=1
        mx=max(mx,end-start+1)
        a.append(i[start:end+1])
        
        
    except:
        break
print('*'*(mx+2))
c=0

for i in a:
    print('*',end='')
    if (mx-len(i))%2==0:
        print(' '*((mx-len(i))//2),end='')
        
        print(i,end='')
        print(' '*((mx-len(i))//2),end='')
    elif (mx-len(i))%2!=0 and c%2==0:
        print(' '*((mx-len(i))//2),end='')
        print(i,end='')
        print(' '*(((mx-len(i))-((mx-len(i))//2))),end='')
        c+=1
    elif (mx-len(i))%2!=0 and c%2==1:
        print(' '*(((mx-len(i))-((mx-len(i))//2))),end='')
        print(i,end='')
        print(' '*((mx-len(i))//2),end='')
        
        
        c+=1
    
    print('*')

print('*'*(mx+2))


        

C++ Code:

// Hydro submission #6411678186e4667c8fe2e831@1678862210157
#include <bits/stdc++.h>
using namespace std;
char s[1005][1005];
string t[1005];
int n = 1, maxlen;
bool delta;
int main()  {
    ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
    char ch;    int l = 0;
    do  {
        l = 0;
        do  {
            ch = getchar();
            if(ch == '\n' || ch == EOF) break;
            t[n].insert(t[n].end(), 1, ch);
        }   while(ch != '\n' && ch != EOF);
        ++n;
    }
    while(ch != EOF);
    --n;
    --n;
    //cout << strlen(s[1]) << endl;
    for(int i = 1; i <= n; ++i) if(t[i].length() > maxlen)   maxlen = t[i].length();

    for(int i = 1; i <= maxlen + 2; ++i)    cout << "*";

    cout << endl;
    for(int i = 1; i <= n; ++i) {
        cout << "*";
        if((maxlen & 1) != (t[i].length() & 1)) {
            for(int j = 1; j <= (maxlen - t[i].length()) / 2 + delta; ++j)  cout << " ";
            cout << t[i];
            for(int j = 1; j <= (maxlen - t[i].length()) / 2 + !delta; ++j) cout << " ";
            delta = !delta;
        }
        else    {
            for(int j = 1; j <= (maxlen - t[i].length()) / 2; ++j) cout << " ";
            cout << t[i];
            for(int j = 1; j <= (maxlen - t[i].length()) / 2; ++j) cout << " ";
        }
        cout << "*" << endl;
    }
    for(int i = 1; i <= maxlen + 2; ++i)    cout << "*";
    cout << endl;
}


Comments

Submit
0 Comments
More Questions

1136A - Nastya Is Reading a Book
1353B - Two Arrays And Swaps
1490E - Accidental Victory
1335A - Candies and Two Sisters
96B - Lucky Numbers (easy)
1151B - Dima and a Bad XOR
1435B - A New Technique
1633A - Div 7
268A - Games
1062B - Math
1294C - Product of Three Numbers
749A - Bachgold Problem
1486B - Eastern Exhibition
1363A - Odd Selection
131B - Opposites Attract
490C - Hacking Cypher
158B - Taxi
41C - Email address
1373D - Maximum Sum on Even Positions
1574C - Slay the Dragon
621A - Wet Shark and Odd and Even
1395A - Boboniu Likes to Color Balls
1637C - Andrew and Stones
1334B - Middle Class
260C - Balls and Boxes
1554A - Cherry
11B - Jumping Jack
716A - Crazy Computer
644A - Parliament of Berland
1657C - Bracket Sequence Deletion